﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Xml;
using System.ServiceModel;
using System.Xml.Linq;
using System.Threading;
using Dsp.Sdk;
using System.Data;
using Dsp.SP.Common;
using Dsp.SP.CustomClient;

namespace FIFP.DashBoard.Web
{
    public partial class ADMA_FIFPWorkflow : AOSdkPage
    {
        string _aoServcieUri = ConfigurationManager.AppSettings[Constants.AOServiceUri];
        public const string UserName = "UserName";
        public const string Email = "Email";
        public const string LoginUserId = "LoginUserId";
        public const string Phone = "Phone";
        public const string AdminEMail = "Mahmoud.Habash@halliburton.com";
        public const string GroupId = "Group_Id";
        string _userId = string.Empty;
        string _userName = string.Empty;
        string _email = string.Empty;
        string _phone = string.Empty;
        string _groupId = string.Empty;
        string _customPageName = "FIFP.ReportTestPage.aspx";//ConfigurationManager.AppSettings[Constants.CustomPageName];
        string _portalUrl = ConfigurationManager.AppSettings[Constants.PortalUrl];
        AutoResetEvent m_autoResetEvent = new AutoResetEvent(false);
        private delegate void DoAction();
        private static string _customMainPageName = "FIFPWorkflow.aspx";

        protected void Page_Load(object sender, EventArgs e)
        {
            _customPageName = "FIFP.ReportTestPage.aspx";
            _customMainPageName = "FIFPWorkflow.aspx";
            GetLoggedInUserDetails();
        }

        private void GetLoggedInUserDetails()
        {
            FIFPGroupUser user = new FIFPGroupUser();
            List<string> parameters = new List<string>();
            parameters.Add(this.User.Identity.Name == String.Empty ? Environment.UserName : this.User.Identity.Name);

            DataTable dtUserInfo = DatabaseQueryService.QueryForData(Constants.DbMsSql, Constants.GET_GROUPUSERS, parameters);
            foreach (DataRow dr in dtUserInfo.Rows)
            {
                user.GroupId = (dr[GroupId] != DBNull.Value) ? Convert.ToString(dr[GroupId]) : string.Empty;
                user.UserName = (dr[UserName] != DBNull.Value) ? Convert.ToString(dr[UserName]) : string.Empty;
                user.Phone = (dr[Phone] != DBNull.Value) ? Convert.ToString(dr[Phone]) : string.Empty;
                user.Email = (dr[Email] != DBNull.Value) ? Convert.ToString(dr[Email]) : string.Empty;
                user.LoginUserId = (dr[LoginUserId] != DBNull.Value) ? Convert.ToString(dr[LoginUserId]) : string.Empty;
            }
            if (user.LoginUserId != null)
            {
                _groupId = user.GroupId;
                _userId = user.LoginUserId;
                _userName = user.UserName;
                _email = !string.IsNullOrEmpty(user.Email) ? user.Email : AdminEMail;
                _phone = user.Phone;
                //initParams.Attributes["value"] = string.Format("AoServicesUri={0},LoginUserId={1},UserName={2},Email={3},Phone={4},CustomPageName={5},CustomMainPageName={6},PortalUrl={7},UserGroupId={8}", _aoServcieUri, _userId, _userName, _email, _phone, _customPageName, _customMainPageName, _portalUrl, _groupId);
            }
            else
            {
                _groupId = string.Empty;
                _userId = this.User.Identity.Name;
                _userName = this.User.Identity.Name;
                if (_userId.Length == 0)
                {
                    _userId = Environment.UserName;
                    _userName = Environment.UserName;

                }
                _email = AdminEMail;
                _phone = string.Empty;



            }
            //_userId = _userId.Substring(_userId.IndexOf("\\") + 1);
            //Response.Write("1. User ID: " + _userId);

            initParams.Attributes["value"] = string.Format("AoServicesUri={0},LoginUserId={1},UserName={2},Email={3},Phone={4},CustomPageName={5},CustomMainPageName={6},PortalUrl={7},UserGroupId={8}", _aoServcieUri, _userId, _userName, _email, _phone, _customPageName, _customMainPageName, _portalUrl, _groupId);
        }
    }

    public class Constants
    {
        public const string DataquerySLBasichttp = "DataQuerySL.svc/basicHttp";
        public const string RelationaDataBasichttp = "RelationalData.svc/basicHttp";
        public const string DbMsSql = "System.Sources.Db.MsSql.FIFP";
        public const string GET_GROUPUSERS = "System.Sources.Db.MsSql.FIFP.Queries.Select_UserDetailsByLoginId.Query";
        public const string Query = "Query";
        public const string AOServiceUri = "AOServiceUri";
        public const string PortalUrl = "PortalUrl";
        public const string CustomPageName = "CustomPageName";
        public const string UserName = "UserName";
        public const string Email = "Email";
        public const string LoginUserId = "LoginUserId";
        public const string Phone = "Phone";
        public const string AdminEMail = "AdminEMail";
    }
    public class FIFPGroupUser
    {
        public string UserId { get; set; }
        public string GroupId { get; set; }
        public string UserName { get; set; }
        public string GroupName { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public bool IsActive { get; set; }
        public string LoginUserId { get; set; }
    }
}